package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.weaponskill.L1WeaponSkillType;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WeaponSkillPowerTable {
  private static final Log _log = LogFactory.getLog(WeaponSkillPowerTable.class);
  
  private static WeaponSkillPowerTable _instance;
  
  private static final Map<Integer, L1WeaponSkillType> _weaponIdIndex = new HashMap<>();
  
  private static final Map<Integer, ArrayList<L1WeaponSkillType>> _weaponSkill = new HashMap<>();
  
  public static WeaponSkillPowerTable get() {
    if (_instance == null)
      _instance = new WeaponSkillPowerTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    load1();
    load2();
    _log
      .info("载入技能武器能力资料数量: " + _weaponIdIndex.size() + "(" + timer.get() + "ms)");
    _log
      .info("载入技能武器设置资料数量: " + _weaponSkill.size() + "(" + timer.get() + "ms)");
  }
  
  private void load1() {
    Connection cn = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      pm = cn.prepareStatement("SELECT * FROM `weapon_skill_power`");
      rs = pm.executeQuery();
      while (rs.next()) {
        int id = rs.getInt("id");
        String classname = rs.getString("classname");
        int level = rs.getInt("level");
        int type1 = rs.getInt("type1");
        int type2 = rs.getInt("type2");
        int type3 = rs.getInt("type3");
        int attr = rs.getInt("attr");
        int ac_mr = rs.getInt("ac_mr");
        int gfxid1 = rs.getInt("gfxid1");
        int gfxid2 = rs.getInt("gfxid2");
        int gfxid3 = rs.getInt("gfxid3");
        int gfxid4 = rs.getInt("gfxid4");
        int gfxid4_count = rs.getInt("gfxid4_count");
        int power = rs.getInt("power");
        int srcdmg = rs.getInt("srcdmg");
        int addsrcdmg = rs.getInt("addsrcdmg");
        int random1 = rs.getInt("random1");
        int random2 = rs.getInt("random2");
        boolean boss_holdout = rs.getBoolean("boss_holdout");
        L1WeaponSkillType class_name = get_class(id, classname);
        if (class_name != null) {
          class_name.set_level(level);
          class_name.set_type1(type1);
          class_name.set_type2(type2);
          class_name.set_type3(type3);
          class_name.set_attr(attr);
          class_name.set_ac_mr(ac_mr);
          class_name.set_gfxid1(gfxid1);
          class_name.set_gfxid2(gfxid2);
          class_name.set_gfxid3(gfxid3);
          class_name.set_gfxid4(gfxid4);
          class_name.set_gfxid4_count(gfxid4_count);
          class_name.set_power(power);
          class_name.set_srcdmg(srcdmg);
          class_name.set_addsrcdmg(addsrcdmg);
          class_name.set_random1(random1);
          class_name.set_random2(random2);
          class_name.set_boss_holdout(boss_holdout);
        } 
        _weaponIdIndex.put(Integer.valueOf(id), class_name);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(cn);
    } 
  }
  
  private void load2() {
    Connection cn = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      pm = cn.prepareStatement("SELECT * FROM `weapon_skill_in`");
      rs = pm.executeQuery();
      while (rs.next()) {
        int item_id = rs.getInt("item_id");
        if (ItemTable.get().getTemplate(item_id) == null) {
          _log.error("技能武器设置资料错误: 没有这个编号的武器:" + item_id);
          continue;
        } 
        String powers = rs.getString("powers").replaceAll(" ", "");
        ArrayList<L1WeaponSkillType> list = _weaponSkill
          .get(Integer.valueOf(item_id));
        if (list == null)
          list = new ArrayList<>(); 
        if (!powers.equals("")) {
          String[] set = powers.split(",");
          int i = 0;
          while (i < set.length) {
            int itemid = Integer.parseInt(set[i]);
            L1WeaponSkillType class_name = _weaponIdIndex
              .get(Integer.valueOf(itemid));
            list.add(class_name);
            i++;
          } 
        } 
        _weaponSkill.put(Integer.valueOf(item_id), list);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(cn);
    } 
  }
  
  private L1WeaponSkillType get_class(int id, String className) {
    if (className.equals("0"))
      return null; 
    try {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.append("com.lineage.server.model.weaponskill.");
      stringBuilder.append(className);
      Class<?> cls = Class.forName(stringBuilder.toString());
      return (L1WeaponSkillType)cls.getMethod("get", new Class[0]).invoke(null, new Object[0]);
    } catch (ClassNotFoundException e6) {
      String error = "发生[技能武器档案]错误, 检查档案是否存在:" + className + " 技能武器编号:" + id;
      _log.error(error);
    } catch (IllegalArgumentException e) {
      _log.error(e.getLocalizedMessage(), e);
    } catch (IllegalAccessException e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } catch (InvocationTargetException e3) {
      _log.error(e3.getLocalizedMessage(), e3);
    } catch (SecurityException e4) {
      _log.error(e4.getLocalizedMessage(), e4);
    } catch (NoSuchMethodException e5) {
      _log.error(e5.getLocalizedMessage(), e5);
    } 
    return null;
  }
  
  public ArrayList<L1WeaponSkillType> getTemplate(int itemid) {
    return _weaponSkill.get(Integer.valueOf(itemid));
  }
}
